package com.bxshey.happyTown.controller;

import com.bxshey.happyTown.dto.cardRolls.AddCardRollsVO;
import com.bxshey.happyTown.entity.User;
import com.bxshey.happyTown.result.Result;
import com.bxshey.happyTown.service.CardRollsService;
import com.bxshey.happyTown.utils.JwtUtils;
import com.bxshey.happyTown.vo.cardRolls.CardRollsVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.util.List;
import java.util.Objects;

/**
 * @作者：不想说话而已
 * @Date：2024/1/4 12:13
 */

@RestController
@RequestMapping("/cardRolls")
public class CardRollsController {

    @Resource
    CardRollsService cardRollsService;

    /**
     * 获取用户所有卡卷,根据卡卷状态
     */
    @GetMapping("/getAll/{statue}")
    public Result<List<CardRollsVO>> getAll(@RequestHeader("Authorization_token") String token,
                                            @PathVariable("statue") Integer statue
    ) {
        User user = JwtUtils.checkToken(token);
        if (Objects.isNull(user)) {
            return Result.error();
        }
        return Result.success(cardRollsService.queryALL(user.getId(), statue));
    }

    /**
     * 新增卡卷信息
     */
    @PostMapping("/insert")
    public Result<?> addCardRolls(@RequestHeader("Authorization_token") String token, @RequestBody AddCardRollsVO cardRolls) {
        User user = JwtUtils.checkToken(token);
        if (Objects.isNull(user)) {
            return Result.error();
        }
        return Result.success(cardRollsService.insertOne(user.getId(), cardRolls.getKind()));
    }

    //获取所有卡卷信息记录
    @GetMapping("/recording")
    public Result<?> recording(@RequestHeader("Authorization_token") String token) {
        User user = JwtUtils.checkToken(token);
        if (Objects.isNull(user)) {
            return Result.error();
        }
        return Result.success(cardRollsService.recording(user.getId()));
    }


    //使用卡卷
    @PutMapping("/choose/{id}")
    public Result<?> choose(@RequestHeader("Authorization_token") String token,@PathVariable("id") Integer id) throws MessagingException {
        User user = JwtUtils.checkToken(token);
        if (Objects.isNull(user)) {
            return Result.error();
        }
        return Result.success(cardRollsService.choose(user.getId(),id));
    }

}
